<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Modules\Blog\Service;


use App\Models\Blog\Message;
use App\Models\System\User;
use Illuminate\Http\Request;

class MessageService
{

    use Service;

    /**
     * 获取最新消息
     *
     * @param  Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function MessageLimitService($request)
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ($query) use ($request, $user) {
            $query->where('p_user_id', $user->id);
            $query->where('static', 1);
        };

        $where2 = function ($query) {
            $query->where('is_show', 1);
            $query->where('status', 1);
        };
        $notice = User::FirstModel(['id' => $user->id])->notice()->where($where2)->orderBy('updated_at', 'desc')
                      ->limit(10);
        switch ($request->input('status')) {
            case 'news':
                $select            = [
                    'id',
                    'static',
                    'is_show',
                    'p_user_id',
                    'article_id',
                    'user_id',
                    'content',
                    'updated_at',
                ];
                $with              = [
                    'user' => function ($query) {
                        $query->select('id');
                        $query->with([
                            'info' => function ($query) {
                                $query->select('id', 'uid', 'avatar', 'username');
                                $query->with([
                                    'file' => function ($query) {
                                        $query->select('id', 'url');
                                    },
                                ]);
                            },
                        ]);
                    },

                ];
                $message[ 'data' ] = Message::AllModel($where, $with, $select);
                break;
            case 'notice':
                $message[ 'data' ] = $notice->get(['id', 'name', 'content', 'is_show', 'status', 'updated_at']);
                break;
            default:
                $message[ 'data' ] = null;
                break;
        }
        $news                            = Message::CountModel($where);
        $message[ 'count' ][ 'notice' ]  = $notice->count();
        $message[ 'count' ][ 'message' ] = $news;
        $message[ 'count' ][ 'count' ]   = calculationNum($news, $notice->count(), '+', 0);
        return data($message);
    }

    /**
     * 公告或消息已读
     *
     * @param  Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function MessageDestroyService($request)
    {
        $user = $this->getCommon()->UserCommonService();
        switch ($request->input('status')) {
            case 'news':
                $id = $request->input('id');
                if ($id != 'all') {
                    $where           = function ($query) use ($id, $user) {
                        $query->where('id', $id);
                        $query->where('user_id', $user->id);
                    };
                    $message         = Message::FirstModel($where, [], ['id', 'static']);
                    $message->static = $request->input('is_show', 2);
                    $message->save();
                } else {
                    $where = function ($query) use ($user) {
                        $query->where('user_id', $user->id);
                    };
                    Message::UpdateModel($where, ['static' => 2]);
                }
                break;
            case 'notice':
                $id = $request->input('id');
                if ($id != 'all') {
                    User::FirstModel(['id' => $user->id])->notice()->detach($id);
                } else {
                    User::FirstModel(['id' => $user->id])->notice()->detach();
                }
                break;
            default:
                return message($this->getErrorCode()::message(1001));
                break;
        }
        return data(null, $this->getErrorCode()::message(210));
    }

    /**
     * 消息
     *
     * @param  Request  $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function MessageIndexService($request)
    {
        $user = $this->getCommon()->UserCommonService();
        switch ($request->input('news')) {
            case 'news':
                $where   = function ($query) use ($request, $user) {
                    $query->where('p_user_id', $user->id);
                    if ($request->filled('is_show')) {
                        $query->where('is_show', $request->input('is_show'));
                    }
                    if ($request->filled('static')) {
                        $query->where('static', $request->input('static'));
                    }
                    if ($request->filled('name')) {
                        $query->whereHas('user', function ($query) use ($request) {
                            $query->whereHas('info', function ($query) use ($request) {
                                $query->where('username', $request->input('name'));
                            });
                        });
                    }
                    if ($request->filled('updated_at')) {
                        $time       = $request->input('updated_at');
                        $start_time = $time[ 0 ];
                        $end_time   = $time[ 1 ];
                        $query->whereBetween('updated_at', [$start_time, $end_time]);
                    }
                };
                $select  = [
                    'id',
                    'static',
                    'is_show',
                    'p_user_id',
                    'article_id',
                    'user_id',
                    'content',
                    'updated_at',
                ];
                $with    = [
                    'user'    => function ($query) {
                        $query->select('id');
                        $query->with([
                            'info' => function ($query) {
                                $query->select('id', 'uid', 'avatar', 'username');
                                $query->with([
                                    'file' => function ($query) {
                                        $query->select('id', 'url');
                                    },
                                ]);
                            },
                        ]);
                    },
                    'article' => function ($query) {
                        $query->select('id', 'title', 'user_id');
                    },
                ];
                $message = Message::GetModel($where, $with, $select, $request->input('limit'), 'static', 'desc');
                break;
            default:
                return message($this->getErrorCode()::message(1001));
                break;
        }
        return data($message);
    }

    /**
     * 批量删除
     *
     * @param $id
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function MessageDeleteService($id)
    {
        $user  = $this->getCommon()->UserCommonService();
        $where = function ($query) use ($id, $user) {
            if (!is_array($id)) {
                $query->where('id', $id);
            } else {
                $query->whereIn('id', $id);
            }
            $query->where('user_id', $user->id);
        };
        Message::DeleteModel($where);
        return data(null, $this->getErrorCode()::message(204));
    }
}
